/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.MENULIST.Customer;

import DAO.DataAccessObject;
import GUI.FUNCTION.CUSTOMER.*;
import GUI.MAINMENU.Home;
import java.awt.Color;
import java.awt.Image;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.ScrollableSizeHint;

/**
 *
 * @author wonka
 */
public class ListCustomer extends org.jdesktop.swingx.JXPanel{
    /**
     * Creates new form Home
     */
    public ListCustomer() {
        initComponents();
        ListSelectionModel model = tableCustomer.getSelectionModel();
        model.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                int index = tableCustomer.getSelectedRow();
                txt_MaKH.setText(tableCustomer.getValueAt(index, 0).toString());
                txt_HoTen.setText(tableCustomer.getValueAt(index, 1).toString());
                txt_NgaySinh.setText(tableCustomer.getValueAt(index, 2).toString());
                txt_GioiTinh.setText(tableCustomer.getValueAt(index, 3).toString());
                txt_CMND.setText(tableCustomer.getValueAt(index, 4).toString());
                txt_DiaChi.setText(tableCustomer.getValueAt(index, 5).toString());
                txt_Email.setText(tableCustomer.getValueAt(index, 6).toString());
                txt_DienThoai.setText(tableCustomer.getValueAt(index, 7).toString());
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jXLabel4 = new org.jdesktop.swingx.JXLabel();
        jXLabel5 = new org.jdesktop.swingx.JXLabel();
        txt_CMND = new org.jdesktop.swingx.JXTextField();
        jXLabel6 = new org.jdesktop.swingx.JXLabel();
        jXLabel7 = new org.jdesktop.swingx.JXLabel();
        jXLabel8 = new org.jdesktop.swingx.JXLabel();
        txt_Email = new org.jdesktop.swingx.JXTextField();
        jXLabel9 = new org.jdesktop.swingx.JXLabel();
        txt_HoTen = new org.jdesktop.swingx.JXTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableCustomer = new org.jdesktop.swingx.JXTable();
        txt_MaKH = new org.jdesktop.swingx.JXTextField();
        txt_NgaySinh = new org.jdesktop.swingx.JXTextField();
        txt_DienThoai = new org.jdesktop.swingx.JXTextField();
        txt_DiaChi = new org.jdesktop.swingx.JXTextField();
        txt_GioiTinh = new org.jdesktop.swingx.JXTextField();

        setBackground(new java.awt.Color(255, 253, 218));
        setScrollableHeightHint(org.jdesktop.swingx.ScrollableSizeHint.NONE);
        setScrollableTracksViewportHeight(false);
        setScrollableTracksViewportWidth(true);
        setScrollableWidthHint(org.jdesktop.swingx.ScrollableSizeHint.NONE);
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jXLabel2.setBackground(new java.awt.Color(153, 255, 255));
        jXLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Medium_ListCustomer.png"))); // NOI18N
        jXLabel2.setText("Danh Sách Khách Hàng");
        jXLabel2.setFont(new java.awt.Font("Segoe UI Semibold", 1, 18)); // NOI18N

        jXPanel1.setBackground(new java.awt.Color(255, 253, 218));
        jXPanel1.setMinimumSize(new java.awt.Dimension(686, 470));
        jXPanel1.setLayout(new java.awt.GridBagLayout());

        jXLabel1.setText("Mã Khách Hàng:");
        jXLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 34, 0, 0);
        jXPanel1.add(jXLabel1, gridBagConstraints);

        jXLabel3.setText("Họ và Tên:");
        jXLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 30, 0, 0);
        jXPanel1.add(jXLabel3, gridBagConstraints);

        jXLabel4.setText("Ngày Sinh:");
        jXLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(26, 34, 0, 0);
        jXPanel1.add(jXLabel4, gridBagConstraints);

        jXLabel5.setText("CMND:");
        jXLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(23, 34, 0, 0);
        jXPanel1.add(jXLabel5, gridBagConstraints);

        txt_CMND.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 261;
        gridBagConstraints.ipady = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 18, 0, 0);
        jXPanel1.add(txt_CMND, gridBagConstraints);

        jXLabel6.setText("Địa chỉ:");
        jXLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(23, 32, 0, 0);
        jXPanel1.add(jXLabel6, gridBagConstraints);

        jXLabel7.setText("Giới Tính");
        jXLabel7.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(26, 30, 0, 0);
        jXPanel1.add(jXLabel7, gridBagConstraints);

        jXLabel8.setText("Email:");
        jXLabel8.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(23, 34, 0, 0);
        jXPanel1.add(jXLabel8, gridBagConstraints);

        txt_Email.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 261;
        gridBagConstraints.ipady = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 18, 0, 0);
        jXPanel1.add(txt_Email, gridBagConstraints);

        jXLabel9.setText("Điện thoại:");
        jXLabel9.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(23, 32, 0, 0);
        jXPanel1.add(jXLabel9, gridBagConstraints);

        txt_HoTen.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 332;
        gridBagConstraints.ipady = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 36);
        jXPanel1.add(txt_HoTen, gridBagConstraints);

        tableCustomer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Mã Khách Hàng", "Họ Và Tên", "Ngày Sinh", "Giới Tính", "CMND", "Địa Chỉ", "Email", "Điện Thoại"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableCustomer.setToolTipText("");
        jScrollPane1.setViewportView(tableCustomer);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.gridwidth = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 804;
        gridBagConstraints.ipady = 204;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(28, 34, 36, 36);
        jXPanel1.add(jScrollPane1, gridBagConstraints);

        txt_MaKH.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 261;
        gridBagConstraints.ipady = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        jXPanel1.add(txt_MaKH, gridBagConstraints);

        txt_NgaySinh.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.ipadx = 261;
        gridBagConstraints.ipady = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(21, 18, 0, 0);
        jXPanel1.add(txt_NgaySinh, gridBagConstraints);

        txt_DienThoai.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 332;
        gridBagConstraints.ipady = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 18, 0, 36);
        jXPanel1.add(txt_DienThoai, gridBagConstraints);

        txt_DiaChi.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 332;
        gridBagConstraints.ipady = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 18, 0, 36);
        jXPanel1.add(txt_DiaChi, gridBagConstraints);

        txt_GioiTinh.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 332;
        gridBagConstraints.ipady = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 18, 0, 36);
        jXPanel1.add(txt_GioiTinh, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 668, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jXPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                .addGap(56, 56, 56))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        // TODO add your handling code here:
          try {
         DefaultTableModel dTM = (DefaultTableModel)tableCustomer.getModel(); 
        dTM.getDataVector().removeAllElements(); 
        tableCustomer.repaint();
        DataAccessObject dao = new DataAccessObject();
        ResultSet res = dao.Select("KHACH_HANG", "MA_KH, HOTEN, NGAYSINH, GIOITINH, SOCMND, DIACHI, EMAIL, SODIENTHOAI ");

        int count = 1;
        while (res.next()) 
        {
            ((DefaultTableModel)((JTable)tableCustomer).getModel()).addRow(new Object[] 
            {
                res.getObject(1), res.getObject(2), res.getObject(3), res.getObject(4), res.getObject(5),res.getObject(6), res.getObject(7),res.getObject(8)
            });
        }
        } catch (Exception ex) {
           JOptionPane.showMessageDialog(this, "Không load được dữ liệu");
        }
    }//GEN-LAST:event_formAncestorAdded

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    private org.jdesktop.swingx.JXLabel jXLabel4;
    private org.jdesktop.swingx.JXLabel jXLabel5;
    private org.jdesktop.swingx.JXLabel jXLabel6;
    private org.jdesktop.swingx.JXLabel jXLabel7;
    private org.jdesktop.swingx.JXLabel jXLabel8;
    private org.jdesktop.swingx.JXLabel jXLabel9;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXTable tableCustomer;
    private org.jdesktop.swingx.JXTextField txt_CMND;
    private org.jdesktop.swingx.JXTextField txt_DiaChi;
    private org.jdesktop.swingx.JXTextField txt_DienThoai;
    private org.jdesktop.swingx.JXTextField txt_Email;
    private org.jdesktop.swingx.JXTextField txt_GioiTinh;
    private org.jdesktop.swingx.JXTextField txt_HoTen;
    private org.jdesktop.swingx.JXTextField txt_MaKH;
    private org.jdesktop.swingx.JXTextField txt_NgaySinh;
    // End of variables declaration//GEN-END:variables

}
